﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class AB45AV95Data : BaseFunction
    {
        #region -------------function -------------------------
        public int ThreadCount;
        public INTEXDataSet setUI;
        string ABPOS = "";
        public AB45AV95Data()
            : base()
        {

        }
        public AB45AV95Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public AB45AV95Data(OracleConnection _OracleDB, IntexReptServBLClient _proxy)
            : base(_OracleDB, _proxy)
        {

        }
        public AB45AV95Data(OracleConnection _OracleDB, IntexReptServBLClient _proxy,INTEXDataSet _setUI)
            : base(_OracleDB, _proxy)
        {
            this.setUI = _setUI;
        }
        ~AB45AV95Data()
        {
            if (OracleDB != null)
            {
                OracleDB.Close();
            }
        }
        public DataSet LoadABPOS(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                string ABPAUFKRS = dwABPOS["ABPAUFKRS"].ToString();
                string ABPAUFLFD = dwABPOS["ABPAUFLFD"].ToString();
                string ABPAUFPOS = dwABPOS["ABPAUFPOS"].ToString();
                string ABPART = dwABPOS["ABPART"].ToString();
                string ABPLIEFNR = dwABPOS["LSNAME1"].ToString().Trim();
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT A.ABPAUFKRS,A.ABPAUFLFD,A.ABPAUFPOS,A.ABPART,A.ABPVAR,A.ABPFARBE, "
                    + " A.ABPAUFLFD AS VWORDERNUM,A.ABPART AS ARTICLE,A.ABPSOLLMENGE1, "
                    + " A.ABPSOLLMENGE8,A.ABPWVDAT,A.ABPLIEFDAT, A.ABPSTARTDAT,A.ABPERLKZ, "
                    + " A.ABPERLDAT, A.ABPLIEFNR, C.LSNAME1 AS SPRODUCER,D.BSFBEZ,0 AS JSACTDATE "
                    + " FROM ABPOS A,ABFOLGE B,LIEFDAT C,BSFARB D "
                    + " WHERE A.ABPMANDANT = B.ABFMANDANT "
                    + " AND A.ABPAUFKRS = B.ABFAUFKRS AND A.ABPAUFLFD = B.ABFAUFLFD "
                    + " AND A.ABPAUFPOS = B.ABFAUFPOS AND B.ABFMANDANT = C.LSMANDANT "
                    + " AND B.ABFLIEFNR = C.LSLIEFNR AND A.ABPMANDANT = D.BSFMANDANT "
                    + " AND A.ABPART = D.BSFKART AND TRIM(ABPVAR) = D.BSFKVAR AND A.ABPFARBE = D.BSFKFARBE "
                    + " AND B.ABFMANUELKZ = 'N' AND TRIM(B.ABFAGANG) = 'SZ' "
                    + " AND A.ABPMANDANT = '00'AND A.ABPAUFART = 'VW' "
                    + " AND A.ABPAUFKRS LIKE '%" + ABPAUFKRS + "%' AND A.ABPAUFLFD LIKE '%" + ABPAUFLFD
                    + "%' AND A.ABPAUFPOS LIKE '%" + ABPAUFPOS + "%' AND A.ABPART LIKE '%" + ABPART + "%' "
                    + " AND A.ABPERLKZ = :ABPERLKZ "
                    + " ORDER BY  SPRODUCER,ABPAUFKRS,ABPAUFLFD,ABPAUFPOS";
                or1.Parameters.Add("ABPERLKZ", OracleType.NVarChar).Value = dwABPOS["ABPERLKZ"].ToString();
                setLocal = Connect2Proxy(or1);

               if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABPOS";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("ABPSOLLMENGE1");
                    Q100.Enqueue("ABPSOLLMENGE8");
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    string column = "";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        column = "ABPFARBE";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        column = "SPRODUCER";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        column = "BSFBEZ";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        dwTemp["VWORDERNUM"] = dwTemp["ABPAUFKRS"].ToString()
                           + "-" + System.Convert.ToInt16(dwTemp["ABPAUFLFD"].ToString()) 
                           + "-" + System.Convert.ToInt16(dwTemp["ABPAUFPOS"].ToString());
                        dwTemp["ARTICLE"] = dwTemp["ABPART"].ToString().Trim()
                            + "-" + dwTemp["ABPFARBE"].ToString().Trim() + "-" + dwTemp["ABPVAR"].ToString().Trim();
                        GetABFOLGE(dwTemp);
                }
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }        
        public DataSet GetVKPOS(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = " SELECT DISTINCT VKPNEUDAT,TRIM(VKPKDARTNR) ||';'|| TRIM(VKPKDBEZ) ||';'|| TRIM(VKPKDFARBNR) AS STDBATCHNO "
                   + " FROM VKPOS A,ABAUFM B,ABAUFM C,ABAUFM D "
                   + " WHERE A.VKPMANDANT = B.ABAMANDANT AND A.VKPSA <> 'AJ' "
                   + " AND A.VKPAUFKRS = B.ABAVKAUFKRS AND A.VKPAUFLFD = B.ABAVKAUFLFD "
                   + " AND A.VKPAUFPOS = B.ABAVKAUFPOS AND B.ABAMANDANT = C.ABAMANDANT "
                   + " AND B.ABAAUFKRS = C.ABAVKAUFKRS AND B.ABAAUFLFD = C.ABAVKAUFLFD "
                   + " AND B.ABAAUFPOS = C.ABAVKAUFPOS AND C.ABAMANDANT = D.ABAMANDANT "
                   + " AND C.ABAAUFKRS = D.ABAVKAUFKRS AND C.ABAAUFLFD = D.ABAVKAUFLFD "
                   + " AND C.ABAAUFPOS = D.ABAVKAUFPOS AND D.ABAAUFKRS = :ABFAUFKRS "
                   + " AND D.ABAAUFLFD = :ABFAUFLFD  AND D.ABAAUFPOS = :ABFAUFPOS "
               ;

                or1.Parameters.Add("ABFAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABFAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABFAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABFOLGE";

                    byte[] b;
                    string value;
                    string column;
                    string VKPNEUDAT = "", STDBATCHNO = "";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        column = "STDBATCHNO";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        VKPNEUDAT = dwTemp["VKPNEUDAT"].ToString().Trim();
                        STDBATCHNO = dwTemp["STDBATCHNO"].ToString().Trim();

                    }
                    dwABPOS["VKPNEUDAT"] = VKPNEUDAT;
                    dwABPOS["STDBATCHNO"] = STDBATCHNO;
                }
                set4Return.Merge(setLocal);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        private DataSet GetGFABRICEINFOByDI(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {

                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT ABPART,ABPVAR,ABPFARBE,P.ABPAUFKRS as WBAUFKRS, "
                    + " P.ABPAUFLFD AS WBAUFLFD,P.ABPAUFPOS AS WBAUFPOS,P.ABPSOLLMENGE1 AS GMETER, " 
                    + " LSLIEFNA,LSNAME1 AS GPRODUCER  FROM ABAUFM M,ABPOS P,LIEFDAT L,ABFOLGE F "
                    + " WHERE M.ABAMANDANT = P.ABPMANDANT AND M.ABAVKAUFKRS = P.ABPAUFKRS "
                    + " AND M.ABAVKAUFLFD = P.ABPAUFLFD AND M.ABAVKAUFPOS = P.ABPAUFPOS "
                    + " AND P.ABPMANDANT = F.ABFMANDANT AND P.ABPAUFKRS = F.ABFAUFKRS AND P.ABPAUFLFD = F.ABFAUFLFD "
                    + " AND P.ABPAUFPOS = F.ABFAUFPOS AND F.ABFMANDANT = L.LSMANDANT "
                    + " AND F.ABFLIEFNR = L.LSLIEFNR AND P.ABPAUFART in ('DI','PR') "
                    + " AND M.ABAAUFKRS=:ABPAUFKRS AND M.ABAAUFLFD=:ABPAUFLFD AND M.ABAAUFPOS=:ABPAUFPOS";
                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABPAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABFOLGE";
                    byte[] b;
                    string value;
                    string sTemp = "", GPRODUCER = "",  column;
                    double GMETER=0;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                       
                        column = "GPRODUCER";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        sTemp = value + ";";
                        if (!GPRODUCER.Contains(sTemp)
                           && sTemp != ";")
                        {
                            GPRODUCER += sTemp;
                        }                        
                        
                        dwABPOS["ARTICLE"] = dwTemp["ABPART"].ToString().Trim()
                           + "-" + dwTemp["ABPFARBE"].ToString().Trim() + "-" + dwTemp["ABPVAR"].ToString().Trim();
                    }
                    GPRODUCER += "***";
                    GPRODUCER = GPRODUCER.Replace(";***", "");
                    GPRODUCER = GPRODUCER.Replace("***", "");
                    GPRODUCER = GPRODUCER.Replace(";", Environment.NewLine);
                    dwABPOS["GPRODUCER"] = GPRODUCER; 
                    Tracer.Verbose(ABPOS + " 织造厂：", GPRODUCER);
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public override DataSet GetReservedOrder(DataRow dwABPOS, ref ArrayList listDIOrder, ref ArrayList listPROrder,string OrderNum="")
        {
            BaseFunction m_Data = new AB50AV95Data();
            m_Data.proxy = this.proxy;
            m_Data.OracleDB = this.OracleDB;
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataTable tableLocal = null;
            DataRow dwNABPOS = dwABPOS.Table.Clone().NewRow();
            try
            {
                dwNABPOS["ABPAUFKRS"] = dwABPOS["ABPAUFKRS"].ToString();
                dwNABPOS["ABPAUFLFD"] = dwABPOS["ABPAUFLFD"].ToString();
                dwNABPOS["ABPAUFPOS"] = dwABPOS["ABPAUFPOS"].ToString();
                setResult = base.GetReservedOrder(dwNABPOS, ref listDIOrder, ref listPROrder, OrderNum);
                if (setResult.Tables.Count > 0)
                {
                    byte[] b;
                    string value;
                    string OrderType = "";
                    string sTemp = "";
                    string SIZEBATCHNO = "";
                    string SPRODUCER = "";
                    foreach (DataRow dwTemp in setResult.Tables["RESERVEDORDER"].Rows)
                    {
                        OrderType = dwTemp["ABPAUFART"].ToString().Trim();
                        if (OrderType != "GP")
                            continue;
                        dwABPOS["JSPENDDATE"] = dwTemp["ABPLIEFDAT"];
                        dwABPOS["JSAENDDATE"] = dwTemp["ABPERLDAT"];            
                    } 
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return setReturn;
        }
        public DataSet GetMATYARNINFO(DataRow dwUIABPOS, DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            string ZUAUFNUM = "";
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT B.GAART FROM GAAUF B "//SUBSTR(B.GAART,INSTR(B.GAART,'.',1)+1) AS GAART FROM GAAUF B "
                    + " WHERE B.GASA = '723' AND B.GAAUFKRS = :ABPAUFKRS "
                    + " AND B.GAAUFLFD = :ABPAUFLFD  AND B.GAAUFPOS = :ABPAUFPOS "  ;

                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwUIABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwUIABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABPAUFPOS", OracleType.NVarChar).Value = dwUIABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    
                    tableLocal.TableName = "GAAUF";
                    SetDefault(tableLocal);
                    string REMARK ="";             
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        REMARK += dwTemp["GAART"].ToString().Trim()+";";
                    }
                    dwABPOS["REMARK"] = REMARK;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public DataSet GetABFOLGE(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                #region code reference
                
                #endregion
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = " SELECT ABFRUECKDAT"
                + " FROM ABFOLGE "
                + " WHERE ABFMANDANT='00'  AND  ABFAUFKRS=:ABFAUFKRS AND ABFAUFLFD=:ABFAUFLFD AND ABFAUFPOS=:ABFAUFPOS "
                + " AND ABFAFOLGE = '010' ";

                or1.Parameters.Add("ABFAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABFAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABFAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);
                
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABFOLGE";

                    byte[] b;
                    string value;
                    string sTemp = "", SPRODUCER = "";
                    Double JSACTDATE = 0;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                         JSACTDATE =  System.Convert.ToDouble(dwTemp["ABFRUECKDAT"].ToString());
                       
                    }
                    dwABPOS["JSACTDATE"] = JSACTDATE;
                    if (JSACTDATE ==0)
                    {
                        dwABPOS["ABPLIEFDAT"] = 0;
                    }
                }
                set4Return.Merge(setLocal);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public DataSet GetBABST(DataRow dwUIABPOS, DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setResult = new DataSet();
            DataTable tableLocal = null;
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT BMBEM,BMSOLLLG, BMSOLLVLG,BMBAUMKRS,BMBAUMLFD FROM BABST M,BABEW W "
                    + " WHERE BMAUFKRS=BBAUFKRS AND BMAUFLFD=BBAUFLFD AND BMAUFPOS=BBAUFPOS "
                    + " AND BMAUFKRS=:BMARFKRS AND BMAUFLFD=:BMAUFLFD AND BMAUFPOS=:BMAUFPOS";
                or1.Parameters.Add("BMARFKRS", OracleType.NVarChar).Value = dwUIABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("BMAUFLFD", OracleType.NVarChar).Value = dwUIABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("BMAUFPOS", OracleType.NVarChar).Value = dwUIABPOS["ABPAUFPOS"].ToString();
                setResult = Connect2Proxy(or1);

                if (setResult.Tables.Count > 0)
                {
                    tableLocal = setResult.Tables[0];
                    tableLocal.TableName = "BABST";
                    Queue Q10 = new Queue();
                    Q10.Enqueue("BMSOLLLG");
                    Q10.Enqueue("BMSOLLVLG");
                    DivideBy10(tableLocal, Q10);
                    byte[] b;
                    string value;
                    string sTemp = "", SPRODUCER = "";
                    double BEAMALENG = 0;
                    int ABEAMNO = 0;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["BMBEM"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["BMBEM"] = value;
                        dwABPOS["BMBEM"] = dwTemp["BMBEM"];
                        BEAMALENG += System.Convert.ToDouble(dwTemp["BMSOLLVLG"].ToString());
                        ABEAMNO++;
                    }
                    double ABPSOLLMENGE1;
                    ABPSOLLMENGE1 = System.Convert.ToDouble(dwABPOS["ABPSOLLMENGE1"].ToString());
                    if (dwABPOS.Table.Columns.Contains("VWBEAMDIFF"))
                    {
                        dwABPOS["VWBEAMDIFF"] = System.Math.Round(ABPSOLLMENGE1 - BEAMALENG, 1);
                        dwABPOS["VWBEAMDIFPEC"] = System.Math.Round(100 * (ABPSOLLMENGE1 - BEAMALENG) / ABPSOLLMENGE1, 2);
                    }

                    dwABPOS["BEAMALENG"] = BEAMALENG;
                    dwABPOS["ABEAMNO"] = ABEAMNO;
                }
                set4Return.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        #endregion
       public override void BuildABPOS(DataRow dwABPOS)
        {
            DataRow dwNABPOS = dwABPOS.Table.Clone().NewRow();
         try
            { 
                dwNABPOS.ItemArray = dwABPOS.ItemArray;
                ABPOS = dwNABPOS["ABPAUFKRS"].ToString() + "." + dwNABPOS["ABPAUFLFD"].ToString() + "." + dwNABPOS["ABPAUFPOS"].ToString();
                ABPOS = ThreadCount.ToString() + ": " + ABPOS;

                string ABAVKAUFTYP = "";
                string LSNAME1 = "";
                string sTemp = "";

                #region----------------------- 查询客单----------------------------
                GetVKPOS(dwNABPOS); 
                #endregion 
                #region----------------------- 查询浆纱回轴------------------------
                //GetBABST(dwNABPOS, dwNABPOS);
                #endregion
                #region----------------------- 查询织造厂----------------------------
                GetGFABRICEINFOByDI(dwNABPOS);
                #endregion

                #region----------------------- 查询后整理厂---------------------------- 
               //GetFFABRICEINFOByDI(dwNABPOS); 
                #endregion

                #region-----------------------经纱信息----------------------------
               GetMATYARNINFO(dwNABPOS, dwNABPOS); 
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            { 
                try
                {
                    dwABPOS.ItemArray = dwNABPOS.ItemArray;
                    string progress = "查询进展:[" + ThreadID.ToString() + "] 完成<-FinishedJobs->/<-TotalJobs->";
                    EventDispatcher.Instance.RaiseStatus(progress, StatusEventType.StartProgress);
                }
                catch (Exception ex)
                {
                    Tracer.Error(ex.StackTrace, ex);
                }
            } 
        }
    }
}